<?php
/**
 * CakeCMS - 后台首页
 * 功能模块:
 *
 * @author zzdboy
 * @version 1.0
 */
class MembersController extends AppController {
	public $components = array (
			'CheckUserPermission' 
	);
	
	/**
	 * 初始化执行
	 *
	 * @see AppController::beforeFilter()
	 */
	public function beforeFilter() {
		parent::beforeFilter ();
		
		// 判断是否登录
		$this->CheckUserPermission->checkLogin ( $this );
		
		$this->set ( 'title_for_layout', 'CakeCMS会员中心管理' );
	}
	public function index() {
		$this->set ( 'title_for_layout', 'CakeCMS会员中心管理' );
		
		// 加载css文件
		$this->set ( 'cssList', array (
				'calendar/jscal2.css',
				'calendar/border-radius.css',
				'calendar/win2k.css' 
		) );
		
		// 加载js文件
		$this->set ( 'scriptList', array (
				'calendar/calendar.js',
				'calendar/lang/en.js',
				'members.js' 
		) );
		
		// 接收分页参数
		if (isset ( $this->params ['named'] ['page'] ) && is_numeric ( $this->params ['named'] ['page'] )) {
			if (floor ( $this->params ['named'] ['page'] ) < 1) {
				$curpage = 1;
			} else {
				$curpage = floor ( $this->params ['named'] ['page'] );
			}
		} else {
			$curpage = 1;
		}
		
		$this->loadModel ( "User" );
		
		// limit
		$limit = ($curpage - 1) * $this->perpage . ', ' . $this->perpage;
		
		// 整理搜索条件
		$conditions = array ();
		
		$email = isset ( $this->params ['named'] ['email'] ) ? $this->params ['named'] ['email'] : '';
		$start_time = isset ( $this->params ['named'] ['start_time'] ) ? $this->params ['named'] ['start_time'] : '';
		$end_time = isset ( $this->params ['named'] ['end_time'] ) ? $this->params ['named'] ['end_time'] : '';
		
		// 整理搜索条件
		$conditions = array ();
		
		if (! empty ( $email )) {
			$conditions ['email'] = $email;
		}
		if (! empty ( $start_time )) {
			$conditions ['ctime >='] = $start_time;
		}
		if (! empty ( $end_time )) {
			$conditions ['ctime <='] = $end_time;
		}
		
		$this->set ( 'email', $email );
		$this->set ( 'start_time', $start_time );
		$this->set ( 'end_time', $end_time );
		
		$list = $this->User->GetUserByList ( $conditions, $limit );
		if ($list && is_array ( $list )) {
			$this->loadModel ( "City" );
			$address = '';
			foreach ( $list ['result'] as &$val ) {
				$City_info = $this->City->GetCityByID ( $val ['User'] ['province'] );
				if ($City_info ['City'] ['name']) {
					$address .= $City_info ['City'] ['name'] . '/';
				}
				
				$City_info = $this->City->GetCityByID ( $val ['User'] ['city'] );
				if ($City_info ['City'] ['name']) {
					$address .= $City_info ['City'] ['name'] . '/';
				}
				
				$City_info = $this->City->GetCityByID ( $val ['User'] ['area'] );
				if ($City_info ['City'] ['name']) {
					$address .= $City_info ['City'] ['name'];
				}
				
				$val ['User'] ['address'] = ! empty ( $address ) ? $address : '未知';
			}
		}
		
		$this->set ( 'list', $list ['result'] );
		// 分页参数
		$this->set ( 'total', $list ['count'] );
		$this->set ( 'perpage', $this->perpage );
		$this->set ( 'curpage', $curpage );
	}
	public function edit() {
		if ($this->request->isPost ()) {
			$status = $this->data ['status'];
			$uid = $this->data ['uid'];
			
			if (! isset ( $status )) {
				$msg = array (
						'rtn_code' => - 1,
						'content' => '请正确选择状态!' 
				);
				echo json_encode ( $msg );
				exit ();
			}
			
			if (empty ( $uid )) {
				$msg = array (
						'rtn_code' => - 1,
						'content' => '参数错误!' 
				);
				echo json_encode ( $msg );
				exit ();
			}
			
			$data ['User'] ['uid'] = $uid;
			$data ['User'] ['status'] = $this->data ['status'];
			
			$this->loadModel ( "User" );
			if ($this->User->edit ( $data )) {
				$msg = array (
						'rtn_code' => 0,
						'content' => '操作成功!' 
				);
				echo json_encode ( $msg );
				exit ();
			} else {
				$msg = array (
						'rtn_code' => - 1,
						'content' => '操作失败!' 
				);
				echo json_encode ( $msg );
				exit ();
			}
		} else {
			$this->set ( 'title_for_layout', 'CakeCMS会员中心管理' );
			
			$uid = $this->params ['uid'];
			
			$this->loadModel ( "User" );
			$UserInfo = $this->User->GetUserByID ( $uid );
			
			$this->set ( 'UserInfo', $UserInfo );
		}
	}
	
	/**
	 * 删除用户
	 */
	public function delete() {
		$uid = $this->data ['uid'];
		
		if (empty ( $uid )) {
			$msg = array (
					'rtn_code' => - 1,
					'content' => '删除失败!' 
			);
			echo json_encode ( $msg );
			exit ();
		}
		
		$this->loadModel ( "User" );
		if ($this->User->del ( $uid )) {
			$msg = array (
					'rtn_code' => 0,
					'content' => '删除成功!' 
			);
			echo json_encode ( $msg );
			exit ();
		} else {
			$msg = array (
					'rtn_code' => 0,
					'content' => '删除失败!' 
			);
			echo json_encode ( $msg );
			exit ();
		}
	}
}
?>